var app = getApp();

Page({
    data: {
        detail: "",
        content: "",
        id:52,
        updateTitle:false
    },
    onLoad: function(a) {
        if(a.hasOwnProperty('id')){
            this.setData({
                id:a.id,
                updateTitle:true
            })
        }
        // var e = this;
        // 1 == a.type && wx.setNavigationBarTitle({
        //     title: "关于我们"
        // }), 2 == a.type && wx.setNavigationBarTitle({
        //     title: "操作指南"
        // }), t.util.request({
        //     url: "entry/wxapp/Api",
        //     data: {
        //         m: "ox_master",
        //         r: "me.webView",
        //         type: a.type,
        //         id: a.id
        //     },
        //     success: function(t) {
        //         var i = t.data.data.content;
        //         e.setData({
        //             detail: t.data.data,
        //             content: i.replace(/\<img/g, '<img style="width:100%;height:auto;disply:block"')
        //         }), 2 != a.type && wx.setNavigationBarTitle({
        //             title: t.data.data.title
        //         });
        //     }
        // });
        this.getrich()
    },

    getrich:function(){
        var that = this
        app.call.getData('index/getArticleInfo?id='+that.data.id,function(data){
            var description = data.data.content;
            if(that.data.updateTitle){
                wx.setNavigationBarTitle({
                    title: data.data.title
                })
            }
            
            that.setData({
                content:that.deCodeRich(description)
            })
        },function(){})
    },
    deCodeRich(str) {
      // 移除iframe
      // const iframeReg = /<iframe[\s\S]*?(\/>|><\/iframe>){1}/g   //原本
      const iframeReg = new RegExp('<iframe[\\s\\S]*?(\\/>|><\\/iframe>){1}', 'g')
      str = str.replace(iframeReg, '')
      // 获取video
      // const videoReg = /<video[\s\S]*?(\/>|><\/video>){1}/g //原本
      const videoReg = new RegExp('<video[\\s\\S]*?(\\/>|><\\/video>){1}', 'g')
      const videoMatch = (str.match(videoReg) || []).map(e => ({ type: 'video', text: e }))
      // 获取audio
      // const audioReg = /<audio[\s\S]*?<\/audio>{1}/g //原本
      const audioReg = new RegExp('<audio[\\s\\S]*?<\\/audio>{1}', 'g')
      const audioMatch = (str.match(audioReg) || []).map(e => ({ type: 'audio', text: e }))
      const matchAry = [].concat(...videoMatch, ...audioMatch)
      let richData = []
      if (matchAry.length > 0) {
        // 匹配到
        for (let m of matchAry) {
          let splitData = str.split(m.text)
          let restOfStr = '' //剩余字符串
          const notMiddle = (splitData[0] ? 1 : 0) ^ (splitData[splitData.length - 1] ? 1 : 0)
          if (!notMiddle) {
            let newSplitData = []
            splitData.forEach((d, i) => {
              newSplitData.push(d)
              if (i !== splitData.length - 1) {
                newSplitData.push('')
              }
            })
            splitData = newSplitData
          }
          const newData = splitData.map(s => {
            if (!s) {
              // 获取 src&style
              // /(?<=src=")[\s\S]*?(?=")/ 原本
              // /(?<=style=")[\s\S]*?(?=")/ 原本
              const srcReg = new RegExp('(?<=src=")[\\s\\S]*?(?=")')
              const styleReg = new RegExp('(?<=style=")[\\s\\S]*?(?=")')
              let src = m.text.match(srcReg)
              let style = m.text.match(styleReg)
              return { type: m.type, src: src ? src[0] : null, style: style ? style[0] : null }
            } else {
              restOfStr += s
              return s
            }
          })
          str = restOfStr
          if (newData.length > 1) {
            //内容正常
            const rIndex = richData.findIndex(rich => typeof rich === "string")
            rIndex !== -1 ? richData[rIndex] = newData : richData = newData
            // 展开二维数组
            richData = Array.prototype.concat.apply([], richData)
          }
          // console.log(richData);
        }
      } else {
        //匹配不到
        richData.push(str)
      }
      this.setData({
        content: richData.map(e => {
          if (typeof e === "string") {
            return { type: 'rich', rich: e }
          } else return Object.assign(e)
        })
      })
    }
});